package com.xmx.ndmallwxapi.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.xmx.common.utils.R;
import com.xmx.ndmallwxapi.feign.*;
import com.xmx.ndmallwxapi.vo.cart.CartAddVo;
import com.xmx.ndmallwxapi.vo.copon.CouponAllVo;
import com.xmx.ndmallwxapi.vo.goods.GoodProductVo;
import com.xmx.ndmallwxapi.vo.order.OrderGoodVo;
import com.xmx.ndmallwxapi.vo.order.OrderVo;

import org.apache.tomcat.util.http.ResponseUtil;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import static com.xmx.common.config.WxResponseCode.*;

@Service
@Aspect
public class OrderService {

    @Resource
    OrderFeignClient orderFeignClient;
    @Resource
    ProductFeignClient productFeignClient;

    @Resource
    UserFeignClient userFeignClient;

    @Resource
    PromotionFeignClient promotionFeignClient;

    @Resource
    CouponFeignClient couponFeignClient;

//    @GlobalTransactional
    public R submitorder(Integer userId,Map<String ,Object> getdata){
        Integer cartId = (Integer) getdata.get( "cartId");
        Integer addressId = (Integer) getdata.get( "addressId");
        Integer couponId = (Integer) getdata.get( "couponId");
        Integer userCouponId = (Integer) getdata.get( "userCouponId");
        String message = (String) getdata.get( "message");
        Integer grouponRulesId = (Integer) getdata.get( "grouponRulesId");
        Integer grouponLinkId = (Integer) getdata.get( "grouponLinkId");


        //TODO:如果是团购项目,验证活动是否有效
        if (grouponRulesId != null && grouponRulesId > 0) {
//            LitemallGrouponRules rules1 = grouponRulesService.findById(grouponRulesId);
            R grouponRuleR = promotionFeignClient.getdetailByid(grouponRulesId);
            //找不到记录
            if (ObjectUtils.isEmpty(grouponRuleR.get("grouponRules"))) {
                return R.badArgument();
            }
            //团购规则已经过期
            Map rules = (Map) grouponRuleR.get("grouponRules");


//            if (rules.get("status").equals(1)) {
//                return R.error(GROUPON_EXPIRED.getCode(), "团购已过期!");
//            }
//            //团购规则已经下线
//            if (rules.get("status").equals(2)) {
//                return R.error(731, "团购已下线!");
//            }

            if (grouponLinkId != null && grouponLinkId > 0) {
                //团购人数已满
//                if(grouponService.countGroupon(grouponLinkId) >= (rules.getDiscountMember() - 1)){
//                    return ResponseUtil.fail(GROUPON_FULL, "团购活动人数已满!");
//                }
//                // NOTE
//                // 这里业务方面允许用户多次开团，以及多次参团，
//                // 但是会限制以下两点：
//                // （1）不允许参加已经加入的团购
//                if(grouponService.hasJoin(userId, grouponLinkId)){
//                    return ResponseUtil.fail(GROUPON_JOIN, "团购活动已经参加!");
//                }
//                // （2）不允许参加自己开团的团购
//                LitemallGroupon groupon = grouponService.queryById(userId, grouponLinkId);
//                // if(groupon.getCreatorUserId().equals(userId)){
//                //     return ResponseUtil.fail(GROUPON_JOIN, "团购活动已经参加!");
//                // }
//                if(groupon!=null) {
//                    if(groupon.getCreatorUserId().equals(userId)){
//                        return ResponseUtil.fail(GROUPON_JOIN, "团购活动已经参加!");
//                    }
                }

        }
            if (cartId == null || addressId == null || couponId == null) {
                return R.badArgument();
            }
        //TODO 收货地址
        R addrR = userFeignClient.findlistByuid(userId, addressId);
        if(ObjectUtils.isEmpty(addrR.get("data"))){
            return R.badArgument();
        }
        Map checkedAddress = (Map) addrR.get("data");
        //TODO 团购优惠
        BigDecimal grouponPrice = new BigDecimal(0);
        R grouponRuleR = promotionFeignClient.getdetailByid(grouponRulesId);
        // 获取团购价格
        if(!ObjectUtils.isEmpty(grouponRuleR.get("grouponRules"))){
            Map map = (Map) grouponRuleR.get("grouponRules");
            grouponPrice = new BigDecimal(String.valueOf(map.get("discount")));
        }
        //TODO 货品价格
        // TODO:  是否选中购物车,将查询出来的商品存放在购物车集合中
        List<CartAddVo> cartAddVoList = new ArrayList<>();

        if (cartId.equals(0)) {
//            checkedGoodsList = cartService.queryByUidAndChecked(userId);
        } else {
            R cartR = orderFeignClient.infoCart(cartId);
            cartAddVoList.add( BeanUtil.mapToBean((Map) cartR.get("cart"),CartAddVo.class,false));
            for (int i = 0; i < cartAddVoList.size(); i++) {
                String specification = cartAddVoList.get(i).getSpecifications()[0];
                cartAddVoList.get(i).setSpecifications(JSONUtil.parseArray(specification).toArray(new String[]{}));
            }
        }
        if (cartAddVoList.size() == 0) {
            return R.badArgumentValue();
        }

        // TODO:  处理团购价格 （计算商品价格
        BigDecimal checkedGoodsPrice = new BigDecimal(0.00);
        //从购物车中取出商品信息获取其价格
        for (CartAddVo cart : cartAddVoList) {
            if(cart==null)
                return R.error("购物车为空");
            if(!ObjectUtils.isEmpty(grouponRuleR.get("grouponRules")) && ((Map)grouponRuleR.get("grouponRules")).get("goodsId").equals(cart.getGoodsId())){
                checkedGoodsPrice = checkedGoodsPrice.add(cart.getPrice().subtract(grouponPrice).multiply(new BigDecimal(cart.getNumber())));
            }else {
                checkedGoodsPrice = checkedGoodsPrice.add(cart.getPrice().multiply(new BigDecimal(cart.getNumber())));
            }
        }

        BigDecimal couponPrice = new BigDecimal(0);
        // 如果couponId=0则没有优惠券，couponId=-1则不使用优惠券
        if (couponId != 0 && couponId != -1) {
            CouponAllVo coupon = checkCoupon( couponId, userCouponId, checkedGoodsPrice, cartAddVoList);
            if (coupon == null) {
                return R.badArgumentValue();
            }
            couponPrice = coupon.getDiscount();
        }

        // 根据订单商品总价计算运费，满88则免运费，否则8元；
        BigDecimal freightPrice = new BigDecimal(0.00);
        if (checkedGoodsPrice.compareTo(new BigDecimal(88)) < 0) {
            freightPrice = new BigDecimal(8);
        }

        // 可以使用的其他钱，例如用户积分
        BigDecimal integralPrice = new BigDecimal(0.00);

        // 订单费用
        BigDecimal orderTotalPrice = checkedGoodsPrice.add(freightPrice).subtract(couponPrice).max(new BigDecimal(0.00));

        BigDecimal actualPrice = orderTotalPrice.subtract(integralPrice);


        Integer orderId = null;
        OrderVo order = new OrderVo();

        order.setUserId(userId);
        order.setOrderSn(generateOrderSn(userId));
        order.setOrderStatus(101);
        order.setConsignee((String) checkedAddress.get("name"));
        order.setMobile((String) checkedAddress.get("tel"));
        order.setMessage(message);
        String detailedAddress = (String) checkedAddress.get("province") + checkedAddress.get("city") + checkedAddress.get("county") + " " + checkedAddress.get("addressDetail");
        order.setAddress(detailedAddress);
        order.setGoodsPrice(checkedGoodsPrice);
        order.setFreightPrice(freightPrice);
        order.setCouponPrice(couponPrice);
        order.setIntegralPrice(integralPrice);
        order.setOrderPrice(orderTotalPrice);
        order.setActualPrice(actualPrice);
        // 有团购
        if (!ObjectUtils.isEmpty(grouponRuleR.get("grouponRules"))) {
            order.setGrouponPrice(grouponPrice);    //  团购价格
        } else {
            order.setGrouponPrice(new BigDecimal(0));    //  团购价格
        }
        // 添加订单表项
        R r = orderFeignClient.addOrder(order);
        orderId = (Integer) r.get("id");


        for (CartAddVo cartAddVo : cartAddVoList) {
            OrderGoodVo orderGoods = new OrderGoodVo();
            orderGoods.setOrderId(orderId);
            orderGoods.setGoodsId(cartAddVo.getGoodsId());
            orderGoods.setGoodsSn(cartAddVo.getGoodsSn());
            orderGoods.setProductId(cartAddVo.getProductId());
            orderGoods.setGoodsName(cartAddVo.getGoodsName());
            orderGoods.setPicUrl(cartAddVo.getPicUrl());
            orderGoods.setPrice(cartAddVo.getPrice());
            orderGoods.setNumber(cartAddVo.getNumber());
            orderGoods.setSpecifications(JSONUtil.toJsonStr(cartAddVo.getSpecifications()));

            orderFeignClient.addOrderGoods(orderGoods);
        }

        // 删除购物车里面的商品信息
        if(cartId.equals(0)){
            orderFeignClient.clearCartGoods(userId);
        }else{
            Integer[] aa = new Integer[]{cartId};
            orderFeignClient.cartdeleteById(aa);
        }

        //TODO 商品货品数量减少
        for (CartAddVo checkGoods : cartAddVoList) {
            Integer productId = checkGoods.getProductId();
            Map map = (Map)productFeignClient.goodsproductinfo(productId).get("goodsProduct");
            GoodProductVo product = BeanUtil.mapToBean(map,GoodProductVo.class,false);

            int remainNumber = product.getNumber() - checkGoods.getNumber();
            if (remainNumber < 0) {
                throw new RuntimeException("下单的商品货品数量大于库存量");
            }

            if (productFeignClient.reduceStock(productId, checkGoods.getNumber() ) == 0) {
                throw new RuntimeException("商品货品库存减少失败");
            }
        }

        int a = 10/0;
        //TODO 如果使用了优惠券，设置优惠券使用状态
        if (couponId != 0 && couponId != -1) {
            Map couponUser = (Map) couponFeignClient.infoUsercoupon(userCouponId).get("couponUser");
            couponUser.replace("status",1);
            couponUser.replace("orderId",orderId);
            couponFeignClient.updateCouponUser(couponUser);
        }
//如果是团购项目，添加团购信息
        if (grouponRulesId != null && grouponRulesId > 0) {
//            LitemallGroupon groupon = new LitemallGroupon();
//            groupon.setOrderId(orderId);
//            groupon.setStatus(GrouponConstant.STATUS_NONE);
//            groupon.setUserId(userId);
//            groupon.setRulesId(grouponRulesId);
//
//            //参与者
//            if (grouponLinkId != null && grouponLinkId > 0) {
//                //参与的团购记录
//                LitemallGroupon baseGroupon = grouponService.queryById(grouponLinkId);
//                groupon.setCreatorUserId(baseGroupon.getCreatorUserId());
//                groupon.setGrouponId(grouponLinkId);
//                groupon.setShareUrl(baseGroupon.getShareUrl());
//                grouponService.createGroupon(groupon);
//            } else {
//                groupon.setCreatorUserId(userId);
//                groupon.setCreatorUserTime(LocalDateTime.now());
//                groupon.setGrouponId(0);
//                grouponService.createGroupon(groupon);
//                grouponLinkId = groupon.getId();
//            }
        }

        // 订单支付超期任务
//        taskService.addTask(new OrderUnpaidTask(orderId));

        Map<String, Object> data = new HashMap<>();
        data.put("orderId", orderId);
        if (grouponRulesId != null && grouponRulesId > 0) {
            data.put("grouponLinkId", grouponLinkId);
        }
        else {
            data.put("grouponLinkId", 0);
        }

        return R.ok().put("data",data);
    }

    public CouponAllVo checkCoupon(Integer couponId, Integer userCouponId, BigDecimal checkedGoodsPrice, List<CartAddVo> cartListi){
        R infocoupon = couponFeignClient.infocoupon(couponId);
        CouponAllVo couponAllVo = BeanUtil.mapToBean((Map) infocoupon.get("coupon"), CouponAllVo.class,true);
        if(couponAllVo == null)
            return null;
        Map couponUser = (Map) couponFeignClient.infoUsercoupon(userCouponId).get("couponUser");
        //如果找不到
        if(couponUser == null) {
            return null;
        }else if(!couponId.equals(couponUser.get("couponId"))){
            return null;
        }

        // 检查是否超期

        LocalDateTime now = LocalDateTime.now();
        if(couponAllVo.getTimeType()==1){
            if(now.isBefore(couponAllVo.getStartTime())|| now.isAfter(couponAllVo.getEndTime())){
                return null;
            }
        }else if(couponAllVo.getTimeType()==0){

            LocalDateTime exprired = LocalDateTime.parse((String)couponUser.get("addTime")).plusDays(couponAllVo.getDays());
            if(now.isAfter(exprired)){
                return null;
            }
        }
        else {
            return null;
        }

        //检测商品是否符合标准
        if(couponAllVo.getGoodsType().equals(1) || couponAllVo.getGoodsType().equals(2)){
            for (CartAddVo cartAddVo : cartListi) {
                Integer key = couponAllVo.getGoodsType().equals(1)? cartAddVo.getGoodsId():0;
            }
        }

        // 检测订单状态

        if (!couponAllVo.getStatus().equals(0)) {
            return null;
        }
        // 检测是否满足最低消费
        if (checkedGoodsPrice.compareTo(couponAllVo.getMin()) == -1) {
            return null;
        }
        return couponAllVo;
    }

    public String generateOrderSn(Integer userId) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        String now = df.format(LocalDate.now());
        String orderSn = now + getRandomNum(6);
        while (orderFeignClient.countByOrderSn(userId, orderSn) != 0) {
            orderSn = now + getRandomNum(6);
        }
        return orderSn;
    }

    private String getRandomNum(Integer num) {
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < num; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }


}
